package com.adams.dcxt.action;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.adams.dcxt.core.Page;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport implements ServletRequestAware, ServletResponseAware {
    private static final long serialVersionUID = 5307122808357830924L;
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    private String size; // 每页显示大小
    private String currentPage; // 当前页

    public void write(String value) {
        try {
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().write(value);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public void setServletResponse(HttpServletResponse response) {
        this.response = response;

    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public String getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(String currentPage) {
        this.currentPage = currentPage;
    }

    public void setPage(Page page) {
        if (StringUtils.isBlank(size)) {
            size = "6";
        }
        if (StringUtils.isBlank(currentPage) || "0".equals(currentPage)) {
            currentPage = "1";
        }
        page.setCurrentPage(Integer.parseInt(currentPage));
        page.setPageSize(Integer.parseInt(size));
        int begincount = (Integer.parseInt(currentPage) - 1) * Integer.parseInt(size);
        page.setBeginCount(begincount);
    }
}
